package com.example.Less.controller;

import com.example.Less.entity.PageResponse;
import com.example.Less.entity.Project;
import com.example.Less.entity.ProjectQuery;
import com.example.Less.service.ProjectService;
import com.example.Less.util.MinioUtil;
import com.github.pagehelper.PageHelper;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/projects")
@RequiredArgsConstructor
public class ProjectController {
    
    private final ProjectService projectService;
    private final MinioUtil minioUtil;

    @PostMapping("/create")
    public ResponseEntity<Project> createProject(@RequestBody Project project) {
        Project savedProject = projectService.saveProject(project);
        return ResponseEntity.ok(savedProject);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Project> getProject(@PathVariable Integer id) {
        Project project = projectService.getProjectById(id);
        return ResponseEntity.ok(project);
    }

    @GetMapping
    public ResponseEntity<List<Project>> getAllProjects() {
        List<Project> projects = projectService.getAllProjects();
        return ResponseEntity.ok(projects);
    }


    @GetMapping("/list")
    public ResponseEntity<List<Project>> getProjects(String name, String addr, String state, String type, int start, int limit ) {
        PageHelper.startPage(start, limit);
        List<Project> projects = projectService.getAllProjects();
        return ResponseEntity.ok(projects);
    }
    @GetMapping("/search")
    public ResponseEntity<PageResponse<Project>> searchProjects(ProjectQuery query) {
        PageResponse<Project> response = projectService.searchProjects(query);
        try {
            for (int i = 0; i < response.getList().size(); i++) {
                String photoNames = response.getList().get(i).getPhoto();
                if (StringUtils.isBlank(photoNames)) {
                    continue;
                }
                String fileUrl = minioUtil.getFileUrl(photoNames, 7);
                response.getList().get(i).setFileUrl(fileUrl);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.ok(response);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Project> updateProject(@PathVariable Integer id, @RequestBody Project project) {
        project.setId(id);
        Project updatedProject = projectService.updateProject(project);
        return ResponseEntity.ok(updatedProject);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProject(@PathVariable Integer id) {
        projectService.deleteProject(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/test")
    public int createData(){




        return 1;
    }

}